<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.phperskill.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace Core\Controller;

/**
 * PES控制器
 * @author LazyPHP
 * @version 1.0
 */
class Controller {

    /**
     * 安全过滤GET提交的数据
     * @param string $name 名称
     * @param boolean $htmlentities 是否转义HTML标签
     * @return string 返回处理完的数据
     */
    protected function g($name, $htmlentities = TRUE) {
        if (is_array($_GET[$name])) {
            return $_GET[$name];
        }
        if ((bool) $htmlentities) {
            $name = htmlspecialchars(trim($_GET[$name]));
        } else {
            $name = trim($_GET[$name]);
        }

        return $name;
    }

    /**
     * 安全过滤POST提交的数据
     * @param string $name 名称
     * @param boolean $htmlentities 是否转义HTML标签
     * @return string 返回处理完的数据
     */
    protected function p($name, $htmlentities = TRUE) {
        if (is_array($_POST[$name])) {
            return $_POST[$name];
        }
        if ((bool) $htmlentities) {
            $name = htmlspecialchars(trim($_POST[$name]));
        } else {
            $name = trim($_POST[$name]);
        }
        return $name;
    }

    /**
     * 判断GET是否有数据提交
     * @param sting $name 名称
     * @param sting $message 返回的提示信息
     * @param boolean $htmlentities 是否转义HTML标签
     */
    protected function is_g($name, $message, $htmlentities = TRUE) {
        //当为0时，直接返回
        if ($_GET[$name] == '0') {
            return $this->g($name, $htmlentities);
        } elseif (is_array($_GET[$name])) {
            return $_GET[$name];
        }
        if (empty($_GET[$name]) || !trim($_GET[$name]) || !is_string($_GET[$name])) {
            $this->error($message);
        } elseif (empty($_GET[$name]) && is_array($_GET[$name])) {
            $this->error($message);
        }
        return $this->g($name, $htmlentities);
    }

    /**
     * 判断POST是否有数据提交
     * @param sting $name 名称
     * @param sting $message 返回的提示信息
     * @param boolean $htmlentities 是否转义HTML标签
     */
    protected function is_p($name, $message, $htmlentities = TRUE) {
        //当为0时，直接返回
        if ($_POST[$name] == '0') {
            return $this->p($name, $htmlentities);
        } elseif (is_array($_POST[$name])) {
            return $_POST[$name];
        }
        if (empty($_POST[$name]) || !trim($_POST[$name]) || !is_string($_POST[$name])) {
            $this->error($message);
        } elseif (empty($_POST[$name]) && is_array($_POST[$name])) {
            $this->error($message);
        }
        return $this->p($name, $htmlentities);
    }

    /**
     * 模板变量赋值
     * @access public
     * @param mixed $name
     * @param mixed $value
     */
    protected function assign($name, $value = '') {
        if (is_array($name)) {
            $this->tVar = array_merge($this->tVar, $name);
        } elseif (is_object($name)) {
            foreach ($name as $key => $val)
                $this->tVar[$key] = $val;
        } else {
            $this->tVar[$name] = $value;
        }
    }

    /**
     * 加载项目主题
     * @param string $theme
     */
    protected function display($theme = '') {
        $funcfile = ITEM . "\\Func\\Func";
        $func = new $funcfile();
        if (!empty($this->tVar)) {
            extract($this->tVar, EXTR_OVERWRITE);
        }
        if (empty($theme)) {
            $file = THEME . '/' . GROUP . '/' . MODULE . '_' . ACTION . '.php';
            $this->checkThemeFileExist($file, MODULE . '_' . ACTION . '.php');
            include $file;
        } else {
            $file = THEME . '/' . GROUP . '/' . $theme . '.php';
            $this->checkThemeFileExist($file, "{$theme}.php");
            include $file;
        }
    }

    /**
     * 检查主题文件是否存在
     */
    protected function checkThemeFileExist($path, $fileName) {
        if (!is_file($path)) {
            $this->error("主题文件{$fileName}丢失!");
        }
    }

    /**
     * 执行成功提示信息
     * @param string $message 信息
     * @param string $url 跳转地址|默认为返回上一页
     * @param int $waitSecond 跳转等待时间
     */
    protected function success($message, $url = 'javascript:history.go(-1)', $waitSecond = '3') {
        self::isAjax(200, $message);
        require PES_CORE . 'Theme/jump.php';
        exit;
    }

    /**
     * 执行失败提信息
     * @param string $message 信息
     * @param string $url 跳转地址|默认为返回上一页
     * @param int $waitSecond 跳转等待时间
     */
    protected function error($error, $url = 'javascript:history.go(-1)', $waitSecond = '3') {
        self::isAjax(0, $error);
        require PES_CORE . 'Theme/jump.php';
        exit;
    }

    /**
     * 返回ajax数据
     * @param type $data 调用数据
     * @param type $code 状态码|默认200
     */
    protected function ajaxReturn($data, $code = '200') {
        self::isAjax($code, $data);
    }

    /**
     * 判断是否ajax提交
     * @param str $code 状态码
     * @param str $info 信息
     * @return boolean|json|xml|str 返回对应的数据类型 
     */
    private static function isAjax($code, $info) {
        if (empty($_SERVER['HTTP_X_REQUESTED_WITH'])) {
            return FALSE;
        }
        $type = explode(',', $_SERVER['HTTP_ACCEPT']);
        $status['status'] = $code;
        $status['info'] = $info;
        switch ($type[0]) {
            case 'application/json':
                exit(json_encode($status));
                break;
            case 'text/javascript':
                // javascript 或 JSONP 格式  需要扩展
                exit();
                break;
            case 'text/html':
                exit($status);
                break;
            case 'application/xml':
                //  XML 格式  需要扩展
                exit();
                break;
        }
    }

}
